package controller

import (
	"github.com/gin-gonic/gin"
	"xdingtec.cn/xdkaika/app"
	"xdingtec.cn/xdkaika/app/http/api/service"
	form "xdingtec.cn/xdkaika/app/http/api/validator/card_template"
	"xdingtec.cn/xdkaika/app/models"
	"xdingtec.cn/xdkaika/utils/response"
)

type CardTemplateController struct {
}

// NewCardTemplateController
func (CardTemplateController) Create(c *gin.Context) {
	var f form.CreateForm
	if err := c.ShouldBind(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	m := &models.CardTemplateModel{
		Title:          f.Title,
		Count:          f.Count,
		Validity:       f.Validity,
		BottomTitle:    f.BottomTitle,
		CreateByUserId: int(app.JWTClaims.UserId),
	}
	ms := &service.CardTemplateService{}
	m, err := ms.Create(m)
	if err != nil || m == nil {
		response.ErrorSystem(c, "新增卡模板失败", gin.H{"err": err})
		return
	}
	response.Success(c, "新增卡模板成功", m)
}

func (CardTemplateController) GetPage(c *gin.Context) {
	var f form.PageForm
	if err := c.ShouldBindQuery(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	ms := &service.CardTemplateService{}
	p, err := ms.GetPage(&f)
	if err != nil {
		response.ErrorSystem(c, "获取列表失败", err)
		return
	}
	response.Success(c, "", p)
}
